One of the biggest problems of software projects is that, while the practice of software development is commonly thought of as engineering, it is inherently a creative discipline; hence, many things about it are hard to measure. While simple yardsticks like test coverage and cyclomatic complexity are important for code quality, what other metrics can we apply to answer questions about our code? What coding conventions or development practices can we implement to make our code easier to measure? We'll take a tour through some processes and tools you can implement to begin improving code quality in your team or organization, and see what a difference it makes to long-term project maintainability. More importantly, we'll look at how we can move beyond today's tools to answer higher-level questions of code quality. Can 'good code' be quantified?
10. What people usually think
Estimating costs
Projecting deadlines
Managerial BS!
11. Client Spec Sheet some paraphrased)
(actual bullet points,
Flash intro with no load time
User account logins, password optional
Ajax chat
“Like Google”
17. Cognitive Dissonance *
Engineers deal with tangible, immutable
constraints, like gravity
The practice of developing software is
an inherently creative discipline
* Thank you, Jones
18. Cognitive Dissonance
Developer constraints (scope, schedule,
budget) potentially / often in flux
Software is inter-related; working on one
part changes the others
No project is exactly the same as another
19. Conclusion
It’s not useful to measure high-level,
intangible things like whole projects
This is where scrum comes in handy
Instead, we can use lower-level, more
concrete measurements
24. Backing up...
What is a metric?
Measurement assigns numbers based on well-
defined meaning
- Sometimes the environment must be
modified
- Special development procedures that track
various activities - Wikipedia (paraphrased)
You can cheat and use booleans, too
25. Notes on continuous integration
A build system
Runs on every code commit
Runs tests
Reports
27. PHP Code Sniffer
PEAR Package:
http://pear.php.net/package/PHP_CodeSniffer
Checks conformance of a set of files against
a series of classes called “sniffs”
31. Measuring code complexity
Cyclomatic complexity
Directly measures the number of linearly
independent paths through a program's
source code.
a.k.a. 1 + the number of times it branches
32. Measuring code complexity
public function render() {
$code = null;
if (isset($this->headers['location']) && $this->status['code'] === 200) {
$code = 302;
}
if (!$status = $this->status($code)) {
throw new Exception('Invalid status code');
}
$this->_writeHeader($status);
foreach ($this->headers as $name => $value) {
$key = strtolower($name);
if ($key == 'location') {
$this->_writeHeader("Location: {$value}", $this->status['code']);
} elseif ($key == 'download') {
$this->_writeHeader('Content-Disposition: attachment; filename="' . $val
} elseif (is_array($value)) {
$this->_writeHeader(
array_map(function($v) use ($name) { return "{$name}: {$v}"; }, $val
);
} elseif (!is_numeric($name)) {
$this->_writeHeader("{$name}: {$value}");
}
}
}
35. Measuring documentation coverage
Check it out:
http://thechaw.com/api_generator
A series of rules
Assigns weights based on docblock content
and various docblock tags
36. Measuring documentation coverage
Basic checks:
Do doc tags exist?
Incomplete @param tags?
Do @param tags match actual params?
Does it have a @link to the man page?
38. Profiling
Get timing / memory usage on every test run
Granular, get statistics per test method
Using continuous integration, code is profiled
on each commit, all on a granular level
44. Finding things to measure
Lithium Inspector class
Lithium Parser class
Based on the awesome work of Sean Coates
http://github.com/scoates/tokalizer